//
//  TJOrderListNewController.m
//  Teleport
//
//  Created by botu　 on 2020/7/24.
//  Copyright © 2020 byx. All rights reserved.
//

#import "TJAdListController.h"
#import "TJAdListCell.h"
#import "JXCategoryView.h"

@interface TJAdListController ()<TJAdListCellDelegate,JXCategoryViewDelegate>
@property (strong, nonatomic) JXCategoryTitleView *categoryView;

@property (strong, nonatomic) NSMutableArray <TJOrderListModel *>*datas;

@property (assign, nonatomic) int pageIndex;
//1,btc 2,usdt
@property (copy, nonatomic) NSString *coinName;


@end
static NSString * const orderListCell = @"orderListCell";
@implementation TJAdListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBar.lineView.hidden = YES;
    
    
    self.navigationBar.title = @"广告列表".localizedString;
    
    
#pragma mark ---------------选择器------------------
    NSArray *titles = @[@"BTC", @"USDT"];
    
    JXCategoryIndicatorLineView *indicatorLineView = [[JXCategoryIndicatorLineView alloc] init];
    indicatorLineView.indicatorHeight = 2;
    indicatorLineView.indicatorColor = UIColor.tj_mainColor;
    indicatorLineView.verticalMargin = 5;
    
    self.categoryView.titles = titles;
    self.categoryView.titleFont = UIFont_Px(15);
    self.categoryView.titleColor = UIColor_Hex(0x262626);
    self.categoryView.titleSelectedColor =UIColor.tj_mainColor;
    self.categoryView.selectedAnimationEnabled = NO;
    self.categoryView.delegate = self;
    self.categoryView.titleLabelZoomEnabled = NO;
    self.categoryView.cellSpacing = 15;
    self.categoryView.frame = CGRectMake(0, KNavBarHight, 122, 40);
    self.categoryView.indicators = @[indicatorLineView];
    self.categoryView.titleColorGradientEnabled = YES;
    [self.view addSubview:self.categoryView];
    
    
    
    /**占位图偏移 **/
    self.verticalOffset = -30;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.rowHeight = 138;
    self.tableView.backgroundColor = UIColor.whiteColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerClass:TJAdListCell.self forCellReuseIdentifier:orderListCell];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.categoryView.mas_bottom);
        make.left.right.offset(0);
        make.height.offset(self.view.tj_height - KNavBarHight - self.categoryView.tj_height);
        
    }];
    
    //默认为第一个
    self.coinName = self.categoryView.titles.firstObject;
    //获取委托单
    [self setUpData];
    
    
    
}
- (void)setUpData{
    [self loadNewData];
    
    self.tableView.mj_header = self.refreshHeader;
    self.tableView.mj_footer = self.refreshFooter;
    
}



#pragma mark -----------------tableViewdegegatel----------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJAdListCell *cell = [tableView dequeueReusableCellWithIdentifier:orderListCell forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.model = self.datas[indexPath.row];
    cell.delegate = self;
    return cell;
}
#pragma mark ---------------TJOrderListCellDelegate------------------
- (void)tapSellLabelWithCell:(TJAdListCell *)cell {
    
    NSDictionary *parameters = @{
        @"orderId": cell.model.id,
        @"userId": TJUserInfo.readInfo.userId,
        
    };
    
    weakify(self);
    [self showloading];
    [TJNetworking.shareNetworking POSTNew:@"otc/order/orderBack" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        [weakself hiddenloading];
        NSIndexPath *indePath = [self.tableView indexPathForCell:cell];
        TJOrderListModel *model =  cell.model;
        model.status = @"FINISH";
        [weakself.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indePath,nil] withRowAnimation:UITableViewRowAnimationNone];
        [TJHud showAutoHideMessage:@"下架成功".localizedString];
        
    } failure:^(NSError * _Nonnull error) {
        [weakself hiddenloading];
        [TJHud showAutoHideMessage:error.localizedFailureReason];
    }];
    
    
}


/**获取委托单*/
- (void)loadNewData{
    _pageIndex = 1;
    
    NSDictionary *parameters = @{
        
        @"currency": self.coinName,
        @"size": @(10),
        @"userId": TJUserInfo.readInfo.userId,
        @"page": @(_pageIndex),
        
    };
    
    weakify(self);
    [TJNetworking.shareNetworking POSTNew:@"otc/order/merchantsOrders" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        
        weakself.displayEmpty = YES;
        
        [weakself.datas removeAllObjects];
        weakself.datas = [TJOrderListModel mj_objectArrayWithKeyValuesArray:data[@"data"]];
        
        [weakself.tableView reloadData];
        [weakself footerViewIsHidden:weakself.datas];
        [weakself.tableView.mj_header endRefreshing];
    } failure:^(NSError * _Nonnull error) {
        [weakself.tableView reloadData];
        [weakself.tableView.mj_header endRefreshing];
    }];
    
}

- (void)loadMoreData {
    _pageIndex++;
    
    NSDictionary *parameters = @{
        
        @"currency": self.coinName,
        @"size": @(10),
        @"userId": TJUserInfo.readInfo.userId,
        @"page": @(_pageIndex),
        
    };
    
    weakify(self);
    [TJNetworking.shareNetworking POSTNew:@"otc/order/merchantsOrders" parameters:parameters success:^(NSDictionary * _Nonnull responseObject, NSDictionary *data) {
        
        NSArray *arr = [TJOrderListModel mj_objectArrayWithKeyValuesArray:data[@"data"]];
        
        [weakself.datas addObjectsFromArray:arr];
        [weakself footerViewIsHidden:arr];
        [weakself.tableView reloadData];
        [weakself.tableView.mj_footer endRefreshing];
        
        
    } failure:^(NSError * _Nonnull error) {
        [weakself.tableView.mj_footer endRefreshing];
    }];
}


- (void)footerViewIsHidden:(NSArray *)arr {
    if (arr.count < 10) {
        self.tableView.mj_footer.hidden = YES;
    }else{
        self.tableView.mj_footer.hidden = NO;
    }
}
- (JXCategoryTitleView *)categoryView {
    if (_categoryView == nil) {
        _categoryView = [[JXCategoryTitleView alloc] init];
    }
    return _categoryView;
}

#pragma mark ---------------JXCategoryViewDelegate------------------
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    
    TJLog(@"index = %ld",index);
    
    self.coinName = self.categoryView.titles[index];
    [self loadNewData];
    
}

@end
